#include <gtest/gtest.h>
#include "common.h"

using namespace ::std;

// class Solution {
// public:
//     ListNode* swapPairs(ListNode* head);
//     ListNode* swapAdjacentPairs(ListNode* left, ListNode* right);
// };

// ListNode* Solution::swapPairs(ListNode* head) {
//     if(nullptr == head || nullptr == head->next) return head;
//     ListNode *left = head, *right = head->next;
//     head = right;
//     left->next = swapPairs(swapAdjacentPairs(left, right));
//     return head;
// }

// // 交换两个节点，并且返回两个节点的下一个节点
// ListNode* Solution::swapAdjacentPairs(ListNode* left, ListNode* right) {
//     left->next = right->next;
//     right->next = left;
//     return left->next;
// }

class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        if(nullptr == head || nullptr == head->next) return head;
        ListNode* result = head->next;
        head->next = swapPairs(result->next);
        result->next = head;
        return result;
    }
};

class SwapPairsCheck : public ::testing::Test {
public:
	class Solution solution;
};

TEST_F(SwapPairsCheck, testSwapPairs_1) {
    vector<int> value = {1,2,3,4,5,6,7};
    printVector(value);
    ListNode* head = makeVecToList(value);
    head = solution.swapPairs(head);
    vector<int> out = makeListToVec(head);
    printVector(out);
    EXPECT_TRUE(vector<int>({2,1,4,3,6,5,7}) == out);
}

int main() {
	::testing::InitGoogleTest();
	return RUN_ALL_TESTS();
}
